package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.annotation.CurrentUser;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.DsmsArea;
import com.csun.dsms.model.domain.DsmsAreaUnit;
import com.csun.dsms.model.domain.DsmsUnit;
import com.csun.dsms.model.domain.DsmsUser;
import com.csun.dsms.model.vo.AreaUnitVo;
import com.csun.dsms.model.vo.menu.MenuVo;
import com.csun.dsms.service.DsmsAreaService;
import com.csun.dsms.service.DsmsAreaUnitService;
import com.csun.dsms.service.DsmsUnitService;
import com.csun.dsms.utils.PublicUtil;
import com.csun.dsms.utils.TreeUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author chisj chisj@foxmail.com
 * @version 1.0.0
 * @date 2021-09-01 11:54
 * @description: TODO
 */
@RestController
@RequestMapping("/unit")
public class DsmsUnitController {

    @Resource
    private DsmsUnitService dsmsUnitServiceImpl;
    @Resource
    private DsmsAreaService dsmsAreaServiceImp;
    @Resource
    private DsmsAreaUnitService dsmsAreaUnitService;

    /*
    *单位的添加
    * */
    @Authorization
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Wrapper unitAdd(@CurrentUser DsmsUser login, @RequestBody DsmsUnit dsmsUnit){
        dsmsUnit.setCreator(login.getLoginName());
        dsmsUnit.setCreatorId(login.getId());
        dsmsUnit.setLastOperator(login.getLoginName());
        dsmsUnit.setLastOperatorId(login.getId());
        dsmsUnit.setUnitType(1);
        /*
        * 判断单位是否重名
        * */
        DsmsUnit unit = new DsmsUnit();
        unit.setName(dsmsUnit.getName());
        List<DsmsUnit> units = dsmsUnitServiceImpl.queryByCondition(unit);
        if(units.size() != 0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"单位名已存在");
        }
        /*
        * 判断社会统一编码是否存在
        * */
        unit.setName("");
        unit.setSocialCode(dsmsUnit.getSocialCode());
        units = dsmsUnitServiceImpl.query(unit);
        if(units.size() != 0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"社会统一信用编码已存在");
        }
        if(dsmsUnit.getPid()==0L){
            DsmsArea dsmsArea = new DsmsArea();
            BeanUtils.copyProperties(dsmsUnit,dsmsArea);
            dsmsAreaServiceImp.save(dsmsArea);
        }
        Integer i = dsmsUnitServiceImpl.save(dsmsUnit);
        DsmsUnit unit1 = dsmsUnitServiceImpl.selectByKey(dsmsUnit.getPid());
        if(unit1.getPid() == 0){
            DsmsArea dsmsArea = new DsmsArea();
            dsmsArea.setPid(0L);
            List<DsmsArea> select = dsmsAreaServiceImp.select(dsmsArea);
            DsmsAreaUnit dsmsAreaUnit = new DsmsAreaUnit();
            dsmsAreaUnit.setAreaId(select.get(0).getId());
            dsmsAreaUnit.setUnitId(dsmsUnit.getId());
            dsmsAreaUnitService.save(dsmsAreaUnit);
        }
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     *单位的删除
     * */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Wrapper unitDelete(@RequestBody DsmsUnit dsmsUnit){
        /*
        * 查询该id是否作为父id
        * */
        DsmsUnit unit = new DsmsUnit();
        unit.setPid(dsmsUnit.getId());
        List<DsmsUnit> units = dsmsUnitServiceImpl.query(unit);
        if(units.size()>0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"该单位下还有子单位");
        }
        Integer i = dsmsUnitServiceImpl.deleteByKey(dsmsUnit);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     *单位的修改
     * */
    @Authorization
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Wrapper unitUpdate(@CurrentUser DsmsUser login,@RequestBody DsmsUnit dsmsUnit){
        /*
         * 判断单位是否重名
         * */
        DsmsUnit unit = new DsmsUnit();
        unit.setName(dsmsUnit.getName());
        List<DsmsUnit> units = dsmsUnitServiceImpl.queryByCondition(unit);
        if(units.size() != 0){
            for (DsmsUnit u:units) {
                if(!u.getId().equals(dsmsUnit.getId())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,"单位名已存在");
                }
            }
        }
        /*
         * 判断社会统一编码是否存在
         * */
        unit.setName("");
        unit.setSocialCode(dsmsUnit.getSocialCode());
        units = dsmsUnitServiceImpl.query(unit);
        if(units.size() != 0){
            for (DsmsUnit u:units) {
                if(!u.getId().equals(dsmsUnit.getId())){
                    return WrapMapper.wrap(Wrapper.ERROR_CODE,"社会统一编码已存在");
                }
            }
        }
        dsmsUnit.setLastOperator(login.getLoginName());
        dsmsUnit.setLastOperatorId(login.getId());
        dsmsUnit.setUpdateTime(new Date());
        Integer i = dsmsUnitServiceImpl.update(dsmsUnit);
        if(i<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     *单位的查询
     * */
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Wrapper unitQuery(@RequestBody DsmsUnit dsmsUnit){
        List<DsmsUnit> dsmsUnits = dsmsUnitServiceImpl.query(dsmsUnit);
        if(dsmsUnits.size() == 0){
            DsmsUnit unit = new DsmsUnit();
            unit.setId(dsmsUnit.getPid());
            unit.setName(dsmsUnit.getName());
            List<DsmsUnit> query = dsmsUnitServiceImpl.query(unit);
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(dsmsUnits));
    }

    /*
    *树状栏菜单
    * */
    @RequestMapping(value = "/tree",method = RequestMethod.POST)
    public Wrapper unitTree(){
        List<DsmsUnit> dsmsUnits = dsmsUnitServiceImpl.query(null);
        List<MenuVo> menuVos = new ArrayList<>();
        for (DsmsUnit dsmsUnit:dsmsUnits) {
            if(dsmsUnit.getUnitType()==1){
                MenuVo menuVo = new MenuVo();
                BeanUtils.copyProperties(dsmsUnit,menuVo);
                menuVo.setMenuName(dsmsUnit.getName());
                menuVos.add(menuVo);
            }
        }
        List<MenuVo> childMenuVos = TreeUtil.getChildMenuVos(menuVos, 0L);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,childMenuVos);
    }

    /*
    * 查询已绑定单位
    * */
    @RequestMapping(value = "/exist",method = RequestMethod.POST)
    public Wrapper unitExist(){
        Set<Long> ids = dsmsUnitServiceImpl.unitExist();
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,ids);
    }

    /*
    * 片区绑定单位获取单位接口
    * */
    @RequestMapping(value = "/bing/unit",method = RequestMethod.POST)
    public Wrapper unitBind(@RequestBody DsmsUnit unit){
        Set<Long> ids = dsmsUnitServiceImpl.unitExist();
        List<DsmsUnit> dsmsUnits = dsmsUnitServiceImpl.query(null);
        if(unit.getUnitIds().size() == 0 && dsmsUnits.size() > 0){
            for (DsmsUnit dsmsUnit:dsmsUnits) {
                if(dsmsUnit.getPid() == 0){
                    unit.getUnitIds().add(dsmsUnit.getId());
                }

            }
        }
        List<MenuVo> menuVos = new ArrayList<>();
        for (DsmsUnit dsmsUnit:dsmsUnits) {
            if(dsmsUnit.getUnitType()==1){
                MenuVo menuVo = new MenuVo();
                BeanUtils.copyProperties(dsmsUnit,menuVo);
                menuVo.setMenuName(dsmsUnit.getName());
                menuVos.add(menuVo);
            }
        }
        List<MenuVo> childMenuVos = TreeUtil.getChildMenuVos(menuVos, 0L);
        List<MenuVo> menuVos1 = unitUtils(childMenuVos, ids,unit);
        DsmsUnit dsmsUnit = new DsmsUnit();
        dsmsUnit.setPid(0L);
        List<DsmsUnit> query = dsmsUnitServiceImpl.query(dsmsUnit);
        if(!unit.getUnitIds().get(0).equals(query.get(0).getId())){
            for (MenuVo menuVo:menuVos1) {
                    unitTreeUtils(menuVo.getSubMenu(),unit);
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,menuVos1);
    }
    //过滤已绑定单位
    public List<MenuVo> unitUtils(List<MenuVo> childMenuVos,Set<Long> ids,DsmsUnit unit){
        Iterator<MenuVo> iterator = childMenuVos.iterator();
        for (Iterator<MenuVo> it = iterator; it.hasNext(); ) {
            MenuVo menuVo = it.next();
            int flag = 0;
            for (Long id:ids) {
                if(unit.getUnitIds() != null){
                    for (Long unitId:unit.getUnitIds()) {
                        if(menuVo.getId().equals(id) && !unitId.equals(id)){
                            flag++;
                        }
                    }
                }
            }
            if(flag > unit.getUnitIds().size()-1){
                try{
                    it.remove();
                }catch (Exception e){

                }
            }
            if(menuVo.isHasMenu()){
                unitUtils(menuVo.getSubMenu(), ids,unit);
            }
        }
        return childMenuVos;
    }
    //移除未绑定单位
    public List<MenuVo> unitTreeUtils(List<MenuVo> childMenuVos,DsmsUnit unit){
        Iterator<MenuVo> iterator = childMenuVos.iterator();
        int flag = 0;
        for (Iterator<MenuVo> it = iterator; it.hasNext(); ){
            MenuVo menuVo = it.next();
            for (Long unitId:unit.getUnitIds()) {
                if(menuVo.getId().equals(unitId)){
                    flag++;
                }
            }
            if(flag == 0){
                try{
                    it.remove();
                }catch (Exception e){

                }

            }
            flag = 0;
        }
        /*for (int i = 0; i < childMenuVos.size(); i++) {
            for (Long unitId:unit.getUnitIds()) {
                if(childMenuVos.get(i).getId().equals(unitId)){
                    flag++;
                }
            }
            if(flag == 0){
                childMenuVos.remove(i);
            }
            flag = 0;
        }*/
        return childMenuVos;
    }

}
